package com.example.smartcitydemo.ui.startUp

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcitydemo.IP
import com.example.smartcitydemo.ui.MainActivity
import com.example.smartcitydemo.MyApplication
import com.example.smartcitydemo.R
import com.example.smartcitydemo.databinding.ActivityStartUp2Binding
import com.example.smartcitydemo.databinding.DialogEditBinding
import com.example.smartcitydemo.databinding.ViewpageItemBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator


class StartUpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        BarUtils.transparentStatusBar(this)
        if (MyApplication.sp.getString(IP, "")!!.isNotEmpty()) {
            setContentView(R.layout.activity_welcome)
            MyApplication.handler.postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            }, 800)
        } else {
            val activityBinding: ActivityStartUp2Binding = DataBindingUtil.setContentView(
                this,
                R.layout.activity_start_up2
            )
            initView(activityBinding)
        }
    }

    private fun initView(activityBinding: ActivityStartUp2Binding) {
        activityBinding.run {
            btnGo.setOnClickListener {
                if (MyApplication.sp.getString(IP, "")!!.isNotEmpty()) {
                    this@StartUpActivity.startActivity(
                        Intent(
                            this@StartUpActivity,
                            MainActivity::class.java
                        )
                    )
                    finish()
                } else {
                    ToastUtils.showLong("请输入IP再进入主页")
                }
            }
            tvSetIp.setOnClickListener {
                var binding: DialogEditBinding
                val dialog = MaterialAlertDialogBuilder(it.context).apply {
                    val inflate: View =
                        LayoutInflater.from(it.context).inflate(R.layout.dialog_edit, null)
                    binding = DataBindingUtil.bind<DialogEditBinding>(inflate)!!
                    setView(inflate)
                    lifecycleOwner = this@StartUpActivity
                    setCancelable(false)
                    setPositiveButton("确认IP", null)
                }.show()
                dialog.let { it1 ->
                    val button = it1.getButton(DialogInterface.BUTTON_POSITIVE)
                    button.setOnClickListener {
                        binding.run {
                            val ipText = textLayout.editText?.text ?: ""
                            val isIp = ipText.isNotEmpty() && RegexUtils.isIP(ipText)
                            if (isIp) {
                                val wholeString =
                                    "${textLayout.prefixText.toString()}$ipText${textLayout.suffixText.toString()}"
                                MyApplication.sp.edit {
                                    putString(IP, wholeString)
                                }
                                it1.dismiss()
                                ToastUtils.showShort(wholeString)
                            } else {
                                textLayout.editText?.text?.clear()
                                ToastUtils.showShort("请输入正确IP")
                            }
                        }

                    }
                }
            }
            mypager2.adapter = MyPageAdapter()
            TabLayoutMediator(mytab, mypager2, object : TabLayoutMediator.TabConfigurationStrategy {
                override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                    tab.setIcon(R.drawable.item_dot)
                }
            }).attach()
            mypager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    if (4 == position) {
                        btnGo.visibility = View.VISIBLE
                        tvSetIp.visibility = View.VISIBLE
                    } else {
                        btnGo.visibility = View.GONE
                        tvSetIp.visibility = View.GONE
                    }
                }
            })
        }

    }
}


class MyPageAdapter : RecyclerView.Adapter<MyPageAdapter.MyViewHolder>() {
    val imgs = arrayOf(
        R.drawable.a1,
        R.drawable.a2,
        R.drawable.a3,
        R.drawable.a4,
        R.drawable.a5,
    )

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val itemBinding = DataBindingUtil.inflate<ViewpageItemBinding>(
            inflater,
            R.layout.viewpage_item,
            parent,
            false
        )
        return MyViewHolder(itemBinding.root)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<ViewpageItemBinding>(holder.itemView)
        binding!!.myImage.setImageResource(imgs[position])
    }

    override fun getItemCount(): Int {
        return imgs.size
    }
}

